from time import sleep
from selenium.webdriver.common.by import By
from Common.Utils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys


class Homepage:
    url=""
    driver=""
    old_count=""
    def __init__(self):
        self.url="http://127.0.0.1:58080/?"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    def search(self):
        #定位搜索框
        search_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input"))
        )

        # 清空并输入关键词
        search_input.clear()
        search_input.send_keys(3)
        #按下 Enter 键
        search_input.send_keys(Keys.ENTER)
        sleep(2)
        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        try:

            assert all("3" in title for title in post_titles), print(f"存在不包含'3'的标题：{[t for t in post_titles if '3' not in t]}")

            print("搜索验证成功！")
        except AssertionError as e:
            BlogDriver.getScreenshort()
            print(f"\u274C 断言失败: {str(e)}")
            raise



    def NightMode(self):
        self.driver.find_element(By.CSS_SELECTOR,"body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg").click()
        sleep(2)
        BlogDriver.getScreenshort()
        # java板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[2]'))
        )
        element.click()
        sleep(2)
        BlogDriver.getScreenshort()

        # C++板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[3]'))
        )
        element.click()
        sleep(2)
        BlogDriver.getScreenshort()

        # MySQL板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[4]'))
        )
        element.click()
        sleep(2)
        BlogDriver.getScreenshort()

        # 面试宝典板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[5]'))
        )
        sleep(2)
        BlogDriver.getScreenshort()

        # 经验分享板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[6]'))
        )
        element.click()
        sleep(2)
        BlogDriver.getScreenshort()

        # 招聘信息板块
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[7]'))
        )
        element.click()

        sleep(2)
        BlogDriver.getScreenshort()

        # 福利待遇板块
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[8]'))
        )
        element.click()
        sleep(2)
        BlogDriver.getScreenshort()

        #帖子详情页
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"
                                 ).click()
        sleep(1)
        BlogDriver.getScreenshort()

        #帖子管理中心
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_profile").click()
        sleep(1)
        BlogDriver.getScreenshort()
        #个人信息
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        self.driver.find_element(By.CSS_SELECTOR, "#index_user_settings").click()
        sleep(1)
        BlogDriver.getScreenshort()



    def DayMode(self):
        element = WebDriverWait(self.driver, 6).until(
            EC.element_to_be_clickable(
                (By.CSS_SELECTOR,
                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light > svg")
            )
        )

        element.click()
        sleep(2)
        BlogDriver.getScreenshort()


    def NavigationBar(self):

        elements_to_check = [
            ("#index_nav_nickname","小小ming"),
            ("#index_nav_name_sub","普通用户"),
            ("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post","发布帖子"),
            ("#nav_board_index > a > span.nav-link-title", "首页"),
            ("#topBoardList > li:nth-child(2) > a > span.nav-link-title", "Java"),
            ("#topBoardList > li:nth-child(3) > a > span.nav-link-title", "C++"),
            ("#topBoardList > li:nth-child(4) > a > span.nav-link-title", "MySQL"),
            ("#topBoardList > li:nth-child(5) > a > span.nav-link-title", "面试宝典"),
            ("#topBoardList > li:nth-child(6) > a > span.nav-link-title", "经验分享"),
            ("#topBoardList > li:nth-child(7) > a > span.nav-link-title", "招聘信息"),
            ("#topBoardList > li:nth-child(8) > a > span.nav-link-title", "福利待遇")
        ]

        # 循环检查所有元素
        for selector, expected_text in elements_to_check:
            try:
                element = WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR, selector))
                )
                actual_text = element.text
                print(f"验证元素 [{selector}] -> 实际内容: {actual_text}")

                # 断言并附带清晰错误信息
                assert actual_text == expected_text, \
                    f"文本不匹配！期望: '{expected_text}'，实际: '{actual_text}'"

            except AssertionError as e:
                print(f"\u274C 断言失败: {str(e)}")
                # 这里可以添加失败截图等操作
            except Exception as e:
                print(f"\u26A0\uFE0F 发生意外错误: {str(e)}")

        print("所有检查项执行完毕，继续后续操作...")


    def Jmp(self):
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        sleep(1)
        BlogDriver.getScreenshort()

        self.driver.find_element(By.CSS_SELECTOR, "#index_user_profile").click()
        sleep(1)
        BlogDriver.getScreenshort()

        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_settings").click()
        sleep(1)
        nickname=self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        print(f"{nickname}")
        assert nickname=="操作成功"
        BlogDriver.getScreenshort()


    def exit(self):
        self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar").click()
        self.driver.find_element(By.CSS_SELECTOR,"#index_user_logout").click()
        BlogDriver.getScreenshort()

    def Postcount(self):
        #java板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[2]'))
        )
        element.click()
        sleep(2)


        old_count=self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles=[title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR,"#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count==f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()


        # C++板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[3]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()

        # MySQL板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[4]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()


        # 面试宝典板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[5]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()

        # 经验分享板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[6]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text


        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]

        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()

        # 招聘信息板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[7]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()

        # 福利待遇板块统计
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="topBoardList"]/li[8]'))
        )
        element.click()
        sleep(2)

        old_count = self.driver.find_element(
            By.CSS_SELECTOR,
            "#article_list_count_board").text

        post_titles = [title.text for title in self.driver.find_elements(
            By.CSS_SELECTOR, "#artical-items-body .text-truncate a strong"
        )]
        count = len(post_titles)
        assert old_count == f"帖子数量: {count}"
        sleep(2)
        BlogDriver.getScreenshort()






